//
//  AllCheckAnimationView.m
//  Ginix
//
//  Created by 冯志强 on 2025/3/9.
//

#import "AllCheckAnimationView.h"

@interface AllCheckAnimationView()
@property (nonatomic,strong) UIImageView * bgView;
@property (nonatomic,strong) UIImageView * iconView;
@property (nonatomic, strong) CABasicAnimation *animation;

@end

@implementation AllCheckAnimationView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self setSubViewLayout];
        [self setupAnimation];
    }
    return self;
}

-(void)setSubViewLayout{
    
    [self addSubview:self.bgView];
    [self addSubview:self.iconView];
    
}

-(void)setSecondStyle{
    
    [self stopAnimation];
    self.iconView.hidden = YES;
    self.bgView.image = [UIImage imageNamed:@"Home_all_check_right_bg"];
    
}

- (void)setupAnimation {
    // 创建基本动画对象，指定要动画的属性为 position.y
    self.animation = [CABasicAnimation animationWithKeyPath:@"position.y"];
    // 设置动画的起始值
    self.animation.fromValue = @(40 * FMScaleX);
    self.animation.toValue = @(200 * FMScaleX);
    // 设置动画持续时间
    self.animation.duration = 1.0;
    // 设置动画重复次数为无限循环
    self.animation.repeatCount = HUGE_VALF;
    // 设置动画完成后是否反向播放，实现上下移动效果
    self.animation.autoreverses = YES;
    // 设置动画完成后不移除，保持最终状态
    self.animation.removedOnCompletion = NO;
}

- (void)startAnimation {
    
    self.iconView.hidden = NO;
    if (![self.iconView.layer animationForKey:@"upDownAnimation"]) {
        // 将动画添加到视图的 layer 上
        [self.iconView.layer addAnimation:self.animation forKey:@"upDownAnimation"];
    }
}

- (void)stopAnimation {
    // 移除指定的动画
    self.iconView.hidden = YES;
    [self.iconView.layer removeAnimationForKey:@"upDownAnimation"];
}

-(UIImageView *)bgView{
    
    if (!_bgView) {
        _bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.getWidth, self.getHeight)];
        _bgView.image = [UIImage imageNamed:@"Home_all_check_left_bg"];
    }
    return _bgView;
}
-(UIImageView *)iconView{
    
    if (!_iconView) {
        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.getWidth, 80 * FMScaleX)];
        _iconView.image = [UIImage imageNamed:@"Home_all_check_center_bg"];
        _iconView.hidden = YES;
    }
    return _iconView;
}
@end
